<template>
  <div>
    <el-card style="margin:20px">
      <CategorySelect @getCategoryId="getCategoryId"></CategorySelect>
    </el-card>
    <el-card style="margin:20px">
      <div v-show="isShowTable">
        <el-button
          type="primary"
          icon="el-icon-plus"
          :disabled="!cForm.category3Id"
          @click="addAttr"
        >添加</el-button>
        <!-- 表格 -->
        <el-table style="width: 100%" border :data="attrInfo">
          <el-table-column type="index" label="序号" width="89" align="center"></el-table-column>
          <el-table-column prop="attrName" label="属性名称" width="150"></el-table-column>
          <el-table-column prop label="属性值列表" width="width">
            <template slot-scope="{row}">
              <el-tag
                type="success"
                v-for="attrValue in row.attrValueList"
                :key="attrValue.id"
                style="margin:2px 10px"
              >{{attrValue.valueName}}</el-tag>
            </template>
          </el-table-column>
          <el-table-column prop="prop" label="操作" width="150">
            <template slot-scope="{row}">
              <el-button type="warning" icon="el-icon-edit" size="mini" @click="edtAttr(row)"></el-button>
              <el-button type="danger" icon="el-icon-delete" size="mini" @click="deleteAttr(row)"></el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <!-- 添加属性或者修改属性的结构 -->
      <div v-show="!isShowTable">
        <el-form :inline="true" ref="form" label-width="80px" :model="addAttrInfo">
          <el-form-item label="属性名">
            <el-input placeholder="请输入属性名" v-model="addAttrInfo.attrName"></el-input>
          </el-form-item>
        </el-form>
        <el-button
          type="primary"
          icon="el-icon-plus"
          @click="addAttrValue"
          :disabled="!addAttrInfo.attrName"
        >添加属性值</el-button>
        <el-button @click="isShowTable = true">取消</el-button>
        <el-table style="width: 100%; margin: 20px 0" border :data="addAttrInfo.attrValueList">
          <el-table-column type="index" label="序号" width="89" align="center"></el-table-column>
          <el-table-column prop="prop" label="属性值名称" width="width">
            <template slot-scope="{row}">
              <el-input v-model="row.valueName" placeholder="请输入属性值名称"></el-input>
            </template>
          </el-table-column>
          <el-table-column prop="prop" label="操作" width="150">
            <template slot-scope="{row, $index}">
              <el-popconfirm :title="`确定删除${row.valueName}吗？`" @onConfirm="deleteAttrValue($index)">
                <el-button type="danger" icon="el-icon-delete" size="mini" slot="reference"></el-button>
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>
        <el-button type="primary" @click="addOrUpdateAttr">保存</el-button>
        <el-button @click="isShowTable = true">取消</el-button>
      </div>
    </el-card>
  </div>
</template>

<script>
export default {
  name: 'Attr',
  data () {
    return {
      cForm: {},
      attrInfo: [],
      isShowTable: true,
      // 收集新增属性|修改属性使用的数据
      addAttrInfo: {
        attrName: "", //属性名
        // 属性值：因为属性值可以有多个因此用数组,每个属性都是一个对象需要attr,valueName
        attrValueList: [],
        categoryId: 0,
        categoryLevel: 3
      }
    }
  },
  methods: {
    async getCategoryId (cForm) {
      this.cForm = cForm;
      const { category1Id, category2Id, category3Id } = cForm;
      let result = await this.$API.attr.reqAttrInfoList(category1Id, category2Id, category3Id);
      // console.log(result.data);
      if (result.code == 200) {
        this.attrInfo = result.data;
      }
    },
    // 添加属性的回调
    addAttr () {
      this.isShowTable = false;
      // 清除上次取消后再次进入留下来的数据
      this.addAttrInfo = {
        attrName: "",
        attrValueList: [],
        categoryId: this.cForm.category3Id,
        categoryLevel: 3
      }

    },
    // 点击添加属性值按钮添加属性值
    addAttrValue () {
      this.addAttrInfo.attrValueList.push({
        attrId: this.addAttrInfo.id, //对于修某个属性的时候，可以在已有的属性值的基础上新增新的属性值（新增属性值的时候，需要把已有的属性id带上）
        valueName: ''
      })
    },
    // 修改属性值的回调
    edtAttr (row) {
      // console.log(row);
      this.isShowTable = false;
      // 由于数据结构当中存在对象里面套数组，数组里面又套对象，因此必须用 深拷贝 才能解决点“取消”不会修改属性值的目的。
      this.addAttrInfo = JSON.parse(JSON.stringify(row));
    },
    // 删除属性的回调
    async deleteAttr(row){
      let result = await this.$API.attr.reqDeleteAttr(row.id)
      console.log(result);
      if(result.code == 200){
        this.$message({type: 'success', message: '删除成功'})
        this.getCategoryId()
      }
    },
    // 气泡确认框按钮的回调
    deleteAttrValue (index) {
      this.addAttrInfo.attrValueList.splice(index, 1)
    },
    // 点击保存按钮的回调
    async addOrUpdateAttr () {
      try {
        let result = await this.$API.attr.reqAddAttrOrUpdateAttr(this.addAttrInfo);
        // console.log(result);
        this.$message({type:'success', message: '保存成功'})
        this.getCategoryId()
        this.isShowTable = true
      } catch (error) {
        this.$messaeg('保存失败')
      }
    }

  },


}
</script>

<style lang="less" scoped>
</style>